package ma.ensao.evalmetrics.view;

import java.awt.Color;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.swing.text.Position;

import ma.ensao.evalmetrics.controller.CaracteristiquesManager;
import ma.ensao.evalmetrics.controller.MethodManager;
import ma.ensao.evalmetrics.controller.MetriquesManager;
import ma.ensao.evalmetrics.controller.ProprieteManager;
import ma.ensao.evalmetrics.controller.ResultManager;
import ma.ensao.evalmetrics.controller.SubcaracManager;
import ma.ensao.evalmetrics.controller.metrics.Calcule;
import ma.ensao.evalmetrics.model.Caracteristiques;
import ma.ensao.evalmetrics.model.Metriques;
import ma.ensao.evalmetrics.model.Propriete;
import ma.ensao.evalmetrics.model.Result;
import ma.ensao.evalmetrics.model.ResultMethod;
import ma.ensao.evalmetrics.model.Subcarac;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.Axis;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.Range;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.Minute;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import com.opensymphony.xwork2.ActionSupport;

/*
 * Préparation du Graphe
 */
public class graphe_fct extends ActionSupport {
	private Result result;
	private Metriques parametres;
	private Caracteristiques carac;
	private Subcarac sub;
	private Propriete propriete;
	private Calcule calcule = new Calcule();
	private MetriquesManager metriquesmanager = new MetriquesManager();
	private ProprieteManager proprietemanager = new ProprieteManager();
	private CaracteristiquesManager caracteristiques = new CaracteristiquesManager();
	private SubcaracManager subcarac = new SubcaracManager();
	private String title;

	private ResultManager resultmanager = new ResultManager();
	private JFreeChart chart;

	public JFreeChart getChart() {
		return chart;
	}

	public void setChart(JFreeChart chart) {
		this.chart = chart;
	}

	static Long id;
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	int param;

	public void param() throws NumberFormatException, IOException {

		File file = new File("list.txt");
		InputStream ips = new FileInputStream(file);
		InputStreamReader ipsr = new InputStreamReader(ips);
		BufferedReader br = new BufferedReader(ipsr);
		String ligne;
		int j = 0;
		while ((ligne = br.readLine()) != null) {
			System.out.println(ligne);
			if (j == 0) {
				id = (long) Integer.parseInt(ligne);
				System.out.println("mm" + id);
			}
			if (j == 1) {
				param = Integer.parseInt(ligne);
				System.out.println("lll" + param);
			}
			j++;
		}
		br.close();
		ipsr.close();
		ips.close();
		// file.delete();
	}

	public String caracteristiques() {
		try {
			param();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(id + "     " + param);
		// Prepare the chart
		List<Result> liste = resultmanager.MesResultat(id);
		TimeSeries pop1 = new TimeSeries("Fonctionnalité", Second.class);
		TimeSeries pop2 = new TimeSeries("Fiabilité", Second.class);
		TimeSeries pop3 = new TimeSeries("Utilisabilité", Second.class);
		TimeSeries pop4 = new TimeSeries("Efficacité", Second.class);
		TimeSeries pop5 = new TimeSeries("Maintenabilité", Second.class);
		TimeSeries pop6 = new TimeSeries("Portabilité", Second.class);

		for (int i = 1; i <= liste.size(); i++) {
			result = liste.get(i - 1);
			System.out.println(result.getLOC());
			long ts = result.getDate().getTime();
			Calendar cal = Calendar.getInstance();
			cal.setTimeInMillis(ts);
			int year = cal.get(Calendar.YEAR);
			int day = cal.get(Calendar.DAY_OF_MONTH);
			int month = cal.get(Calendar.MONTH) + 1;

			parametres = metriquesmanager.list(id).get(0);
			parametres = calcule.calculMetrique(parametres, result);
			propriete = proprietemanager.list(id).get(0);
			propriete = calcule.calculPropriete(propriete);
			sub = subcarac.list(id).get(0);
			sub = calcule.calculSubcarac(sub);
			carac = caracteristiques.list(id).get(0);
			carac = calcule.calculCaracteristiques(carac);
			System.out.println("fiab"+carac.getReliabilite());

			pop1.add(new Second(result.getDate().getSeconds(), result.getDate()
					.getMinutes(), result.getDate().getHours(), day, month,
					year), carac.getFonctionnalite());
			pop2.add(new Second(result.getDate().getSeconds(), result.getDate()
					.getMinutes(), result.getDate().getHours(), day, month,
					year), carac.getReliabilite());
			pop3.add(new Second(result.getDate().getSeconds(), result.getDate()
					.getMinutes(), result.getDate().getHours(), day, month,
					year), carac.getUsabilite());
			pop4.add(new Second(result.getDate().getSeconds(), result.getDate()
					.getMinutes(), result.getDate().getHours(), day, month,
					year), carac.getEfficacite());
			pop5.add(new Second(result.getDate().getSeconds(), result.getDate()
					.getMinutes(), result.getDate().getHours(), day, month,
					year), carac.getMaintenabilite());
			pop6.add(new Second(result.getDate().getSeconds(), result.getDate()
					.getMinutes(), result.getDate().getHours(), day, month,
					year), carac.getPortabilite());

		}

		TimeSeriesCollection dataset = new TimeSeriesCollection();
		dataset.setDomainIsPointsInTime(true);

		dataset.addSeries(pop1);
		dataset.addSeries(pop2);
		dataset.addSeries(pop3);
		dataset.addSeries(pop4);
		dataset.addSeries(pop5);
		dataset.addSeries(pop6);

		chart = ChartFactory.createTimeSeriesChart("Caractéristiques", "Date",
				"Valeur", dataset, true, true, false);
		XYPlot plot = chart.getXYPlot();
		DateAxis axis = (DateAxis) plot.getDomainAxis();
		plot.setBackgroundPaint(new Color(0xffffe0));
		plot.setDomainGridlinesVisible(true);
		plot.setDomainGridlinePaint(Color.lightGray);
		plot.setRangeGridlinesVisible(true);
		plot.setRangeGridlinePaint(Color.lightGray);
		plot.setDomainCrosshairVisible(true);
		plot.setRangeCrosshairVisible(false);
		final XYItemRenderer xyitemrenderer = plot.getRenderer();
		if (xyitemrenderer instanceof XYLineAndShapeRenderer) {
			final XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) xyitemrenderer;
			renderer.setBaseShapesFilled(true);
			renderer.setBaseShapesVisible(true);
			renderer.setShapesVisible(true);
			renderer.setDrawOutlines(true);
		}

		axis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yy '\n' HH:mm"));

		System.err.println("fin chart");

		return SUCCESS;
	}

	public String execute() {

		try {
			param();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(id + "     " + param);
		// Prepare the chart
		List<Result> liste = resultmanager.MesResultat(id);

		TimeSeries pop = new TimeSeries("Evolution", Second.class);

		// System.out.println("jjjjjjjjjjjjjjjjj>>>"+h.getHour());
		for (int i = 1; i <= liste.size(); i++) {
			result = liste.get(i - 1);
			long ts = result.getDate().getTime();
			Calendar cal = Calendar.getInstance();
			cal.setTimeInMillis(ts);
			int year = cal.get(Calendar.YEAR);
			int day = cal.get(Calendar.DAY_OF_MONTH);
			int month = cal.get(Calendar.MONTH) + 1;

			parametres = metriquesmanager.list(id).get(0);
			parametres = calcule.calculMetrique(parametres, result);
			propriete = proprietemanager.list(id).get(0);
			propriete = calcule.calculPropriete(propriete);
			sub = subcarac.list(id).get(0);
			sub = calcule.calculSubcarac(sub);
			carac = caracteristiques.list(id).get(0);
			carac = calcule.calculCaracteristiques(carac);

			if (param == 13) {// interoperabilite

				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), sub.getInteroperabilite());
				title = "Evolution de la sous-caractéristique l'Intéropérabilité";
			}
			if (param == 131) {// heritage
				title = "Evolution de la propriété de l'Héritage";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), propriete.getHeritage());
			}
			if (param == 1311) {// heritage noc
				title = "Evolution de la métrique NOC";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), parametres.getNOC());
			}
			if (param == 1312) {// heritage dit
				title = "Evolution de la métrique DIT";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), parametres.getDIT());
			}
			if (param == 14) {// securite
				title = "Evolution de la sous-caractéristique de la Sécurité";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), sub.getSecurite());
			}
			if (param == 141) {// abstraction
				title = "Evolution de la propriété de l'Abstraction";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), propriete.getAbstraction());
			}
			if (param == 1411) {// heritage noc
				title = "Evolution de la métrique NOC";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), parametres.getAbs());
			}
			if (param == 143) {// size
				title = "Evolution de la propriété de la taille";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), propriete.getSize());
			}
			if (param == 1431) {// size loc
				title = "Evolution de la métrique LOC";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), parametres.getLOC());
			}
			if (param == 1432) {// size cloc
				title = "Evolution de la métrique CLOC";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), parametres.getCLOC());
			}
			if (param == 1433) {// size nom
				title = "Evolution de la métrique NOM";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), parametres.getNOM());
			}
			if (param == 146) {// encapsulation
				title = "Evolution de la propriété d'Encapsulation";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), propriete.getEncapsulation());
			}
			if (param == 1461) {// encapsulati ahf
				title = "Evolution de la métrique AHF";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), parametres.getAHF());
			}
			if (param == 1462) {// encapsulati mhf
				title = "Evolution de la métrique MHF";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), parametres.getMHF());
			}
			if (param == 214) {// cohesion
				title = "Evolution de la propriété de Cohésion";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), propriete.getCohesion());
			}
			if (param == 2141) {// tcc
				title = "Evolution de la métrique TCC";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), parametres.getTCC());
			}
			if (param == 23) {// recouvrabilite
				title = "Evolution de la sous-caractéristique de Recouvrabilité";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), sub.getRecoverabilite());
			}
			if (param == 231) {// complexite
				title = "Evolution de la sous-caractéristique de Compléxité";

				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), propriete.getComplexite());
			}
			if (param == 2311) {// complexite wmc
				title = "Evolution de la métrique WMC";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), parametres.getWMC());
			}
			if (param == 2312) {// complexite cyclo
				title = "Evolution de la métrique Cyclomatique";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), parametres.getCyclo());
			}
			if (param == 333) {// message
				title = "Evolution de la propriété du Message";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), propriete.getMessage());
			}
			if (param == 3331) {// message rfc
				title = "Evolution de la métrique RFC";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), parametres.getRFC());
			}
			if (param == 3332) {// message mpc
				title = "Evolution de la métrique MPC";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), parametres.getMPC());
			}
			if (param == 52) {// variabilite
				title = "Evolution de la sous-caractéristique Changeabilité";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), sub.getVariabilite());
			}
			if (param == 51) {// Analyse
				title = "Evolution de la sous-caractéristique Analyse";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), sub.getAnalyse());
			}
			if (param == 54) {// getTestabilite
				title = "Evolution de la sous-caractéristique Téstabilité";

				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), sub.getTestabilite());
			}
			if (param == 515) {// nbmethodes
				title = "Evolution de la propriété Nombre de Méthodes";

				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), propriete.getNbmethodes());
			}
			if (param == 5151) {// apf
				title = "Evolution de la métriique APF";

				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), parametres.getAPF());
			}
			if (param == 5152) {// mpf
				title = "Evolution de la métriique MPF";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), parametres.getMPF());
			}
			if (param == 1) {//
				title = "Evolution de la caractéristique Fonctionnalité";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), carac.getFonctionnalite());
			}
			if (param == 2) {//
				title = "Evolution de la caractéristique Fiabilité";
				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), carac.getReliabilite());
			}
			if (param == 3) {//
				title = "Evolution de la caractéristique Utilisabité";

				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), carac.getUsabilite());
			}
			if (param == 4) {//
				title = "Evolution de la caractéristique Efficacité";

				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), carac.getEfficacite());
			}
			if (param == 5) {//
				title = "Evolution de la caractéristique Maintenabilité";

				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), carac.getMaintenabilite());
			}
			if (param == 6) {//
				title = "Evolution de la caractéristique Portabilité";

				pop.add(new Second(result.getDate().getSeconds(), result
						.getDate().getMinutes(), result.getDate().getHours(),
						day, month, year), carac.getPortabilite());
			}

		}

		TimeSeriesCollection dataset = new TimeSeriesCollection();
		dataset.setDomainIsPointsInTime(true);

		dataset.addSeries(pop);
		chart = ChartFactory.createTimeSeriesChart(title, "Date", "Valeur",
				dataset, true, true, false);
		XYPlot plot = chart.getXYPlot();
		DateAxis axis = (DateAxis) plot.getDomainAxis();
		plot.setBackgroundPaint(new Color(0xffffe0));
		plot.setDomainGridlinesVisible(true);
		plot.setDomainGridlinePaint(Color.lightGray);
		plot.setRangeGridlinesVisible(true);
		plot.setRangeGridlinePaint(Color.lightGray);
		plot.setDomainCrosshairVisible(true);
		plot.setRangeCrosshairVisible(false);
		final XYItemRenderer xyitemrenderer = plot.getRenderer();
		if (xyitemrenderer instanceof XYLineAndShapeRenderer) {
			final XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) xyitemrenderer;
			renderer.setBaseShapesFilled(true);
			renderer.setBaseShapesVisible(true);
			renderer.setShapesVisible(true);
			renderer.setDrawOutlines(true);
		}
		axis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yy '\n' HH:mm"));

		return SUCCESS;
	}

	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		
		graphe_fct gr=new graphe_fct();
		gr.setId((long) 87);
		gr.caracteristiques();
		 
	}
	
}
